package net.blank.soundMgt
{
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	public class SoundManagement
	{
		/**静态实例*/
		private static var $instance:SoundManagement;
		
		/**背景声效控制*/
		protected var backGroundSoundTransform:SoundTransform=new SoundTransform();
		/**背景声效*/
		protected var backGroundSoundChannel:SoundChannel;
		
		/**前景声效控制*/
		protected var foreGroundSoundTransform:SoundTransform=new SoundTransform();
		/**前景声效*/
		protected var foreGroundSoundChannel:SoundChannel;
		
		/**获取 SoundManagement 实例*/
		public static function getInstance():SoundManagement{
			if(!$instance){
				$instance=new SoundManagement();
			}
			return $instance;
		}
		
		/**停止背景声效的播放*/
		public function stopBackGroundSound():void{
			if(backGroundSoundChannel){
				backGroundSoundChannel.stop();
			}
		}
		
		/**播放背景声效*/
		public function playBackGroundSound(sound:Sound,isLoop:Boolean=true):void{
			stopBackGroundSound();
			
			if(isLoop){
				backGroundSoundChannel=sound.play(0,int.MAX_VALUE,backGroundSoundTransform);
			}else{
				backGroundSoundChannel=sound.play(0,0,backGroundSoundTransform);
			}
		}
		
		/**设置背景声效音量*/
		public function setBackGroundSoundVolume(volume:Number):void{
			backGroundSoundTransform.volume=volume;
			backGroundSoundChannel.soundTransform=backGroundSoundTransform;
		}
		
		/**停止前景声效的播放*/
		public function stopForeGroundSound():void{
			if(foreGroundSoundChannel){
				foreGroundSoundChannel.stop();
			}
		}
		
		/**播放前景声效*/
		public function playForeGroundSound(sound:Sound,loopNum:uint=0):void{
			//stopForeGroundSound();
			
			foreGroundSoundChannel=sound.play(0,loopNum,foreGroundSoundTransform);
		}
		
		/**设置前景声效音量*/
		public function setForeGroundSoundVolume(volume:Number):void{
			foreGroundSoundTransform.volume=volume;
			foreGroundSoundChannel.soundTransform=foreGroundSoundTransform;
		}
		
		public function SoundManagement(){
			if($instance){
				throw new Error("只能用getInstance()来获取实例");
			}
		}
	}
}